from django.db import models
from django.utils import timezone
from common.models.auth_user import AuthUser

class VipPackage(models.Model):
    """VIP套餐模型"""
    name = models.CharField(max_length=50, verbose_name='套餐名称')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    duration = models.IntegerField(verbose_name='时长(天)')
    description = models.TextField(verbose_name='描述')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        app_label = 'common'
        db_table = 'vip_package'
        verbose_name = 'VIP套餐'
        verbose_name_plural = verbose_name
        ordering = ['price']

    def __str__(self):
        return self.name

class VipOrder(models.Model):
    """VIP订单模型"""
    PAYMENT_STATUS_CHOICES = (
        ('pending', '待支付'),
        ('paid', '已支付'),
        ('cancelled', '已取消'),
    )
    
    PAYMENT_METHOD_CHOICES = (
        ('alipay', '支付宝'),
        ('wechat', '微信支付'),
    )
    
    user = models.ForeignKey(AuthUser, on_delete=models.CASCADE, verbose_name='用户')
    package = models.ForeignKey(VipPackage, on_delete=models.CASCADE, verbose_name='套餐')
    order_no = models.CharField(max_length=50, unique=True, verbose_name='订单号')
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='金额')
    payment_method = models.CharField(max_length=20, choices=PAYMENT_METHOD_CHOICES, verbose_name='支付方式')
    payment_status = models.CharField(max_length=20, choices=PAYMENT_STATUS_CHOICES, default='pending', verbose_name='支付状态')
    paid_at = models.DateTimeField(null=True, blank=True, verbose_name='支付时间')
    expire_at = models.DateTimeField(null=True, blank=True, verbose_name='过期时间')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        app_label = 'common'
        db_table = 'vip_order'
        verbose_name = 'VIP订单'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.package.name}"

class OperationLog(models.Model):
    """操作日志模型"""
    user = models.ForeignKey(AuthUser, on_delete=models.CASCADE, verbose_name='用户')
    action = models.CharField(max_length=50, verbose_name='操作')
    target = models.CharField(max_length=50, verbose_name='操作对象')
    target_id = models.IntegerField(verbose_name='对象ID')
    detail = models.TextField(verbose_name='详细信息')
    ip = models.GenericIPAddressField(verbose_name='IP地址')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        app_label = 'common'
        db_table = 'operation_log'
        verbose_name = '操作日志'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.action}" 